package com.daffodil.workable.runner;

import javax.sql.DataSource;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import com.daffodil.workable.schedule.WorkableScheduleThread;
import com.daffodil.workable.schedule.WorkableTriggerThread;

/**
 * 
 * @author yweijian
 * @date 2022年4月27日
 * @version 1.0
 * @description
 */
@Component
public class WorkableApplicationRunner implements ApplicationRunner, InitializingBean, DisposableBean {

    /** 可拖拽任务执行线程池核心值 */
    @Value("${workable.thread-pool.core-size:10}")
    private int corePoolSize;

    /** 可拖拽任务执行线程池最大值 */
    @Value("${workable.thread-pool.max-size:10}")
    private int maximumPoolSize;

    /** 可拖拽任务执行队列最大值 */
    @Value("${workable.thread-pool.queue-size:1000}")
    private int blockingQueueSize;

    @Autowired
    private DataSource dataSource;

    private WorkableTriggerThread workableTriggerThread;

    private WorkableScheduleThread workableScheduleThread;

    @Override
    public void run(ApplicationArguments args) throws Exception {

        this.workableTriggerThread = WorkableTriggerThread.getInstance();
        this.workableTriggerThread.init(corePoolSize, maximumPoolSize, blockingQueueSize);
        this.workableTriggerThread.start();

        this.workableScheduleThread = WorkableScheduleThread.getInstance();
        this.workableScheduleThread.init(dataSource, blockingQueueSize);
        this.workableScheduleThread.start();
    }

    @Override
    public void destroy() throws Exception {
        this.workableScheduleThread.stop();
        this.workableTriggerThread.stop();
    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }

}
